package com.dingreading.cloud.mall.service.impl;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.AccountUtil;
import com.dingreading.cloud.common.util.EnumOperationType;
import com.dingreading.cloud.mall.dto.CategoryDto;
import com.dingreading.cloud.mall.entity.GoodsCategory;
import com.dingreading.cloud.mall.entity.table.GoodsCategoryTableDef;
import com.dingreading.cloud.mall.mapper.GoodsCategoryMapper;
import com.dingreading.cloud.mall.service.GoodsCategoryService;
import com.dingreading.cloud.mall.service.StoresService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class GoodsCategoryServiceImpl extends BaseServiceImpl<GoodsCategoryMapper, GoodsCategory, GoodsCategoryTableDef> implements GoodsCategoryService {

    @Override
    protected GoodsCategoryTableDef getTable() {
        return GoodsCategoryTableDef.goodsCategory;
    }

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;
    @Resource
    private StoresService storesService;


    @Override
    public R<Object> getCategoryByGoodsPower(String storeUid, Integer operationType, List<Integer> cooperationTypeList) {
        // 获取门店的服务
        List<Long> subjectIdList = storesService.getSubjectIdList(storeUid);
        if (CollectionUtils.isEmpty(subjectIdList))
            return R.fail("门店未分配科目，请联系运营人员");

        String cooperationSql = AccountUtil.getCooperationTypeSql2(cooperationTypeList);
        String operationTypeSql = AccountUtil.getOperationTypeSql2(operationType);

        boolean easyProductJoinType = operationType.equals(EnumOperationType.EASY_PRODUCT_JOIN.getType());
        String subjectTypeSql = "";
        if (CollectionUtils.isNotEmpty(subjectIdList)) {
            String subjectIdSql = subjectIdList.stream().map(String::valueOf).collect(Collectors.joining("|"));
            subjectTypeSql = "(^|,)(" + subjectIdSql + ")(,|$)";
        }

        List<CategoryDto> list = goodsCategoryMapper.getCategoryByGoodsPowerV5(
                easyProductJoinType, operationType, subjectTypeSql, cooperationSql, operationTypeSql
        );
        return R.ok(list);
    }

}
